package com.eqdd.yiqidian.ui.select;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.SelectMultiStaffActivityCustom;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.EmploymentListResultBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.utils.ImageUtil;
import com.eqdd.yiqidian.utils.SwipeUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.jakewharton.rxbinding.view.RxView;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

public class SelectMultiStaffActivity extends BaseActivity {
    private SelectMultiStaffActivityCustom dataBinding;
    private SlimAdapterEx<EmploymentListResultBean> slimAdapterEx;
    private String departId;
    private String companyId;
    private ArrayList<EmploymentListResultBean> datas;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_select_multi_staff);
        initTopTitleBar(View.VISIBLE, "人员选择");
        initTopRightText("确定", v -> {
                Intent intent = new Intent();
                intent.putExtra("content", slimAdapterEx.getData().size()==0?"":slimAdapterEx.getData().size() + "人");
                intent.putExtra("guids", getGuids());
                intent.putParcelableArrayListExtra("list", (ArrayList<EmploymentListResultBean>) slimAdapterEx.getData());
                setResult(Config.INTENT_CONTENT, intent);
                finish();
        });
    }

    private String getGuids() {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < slimAdapterEx.getData().size(); i++) {
            stringBuilder.append(slimAdapterEx.getDataItem(i).getGuid() + ";");
        }
        return slimAdapterEx.getData().size()==0?"":stringBuilder.deleteCharAt(stringBuilder.lastIndexOf(";")).toString();
    }

    @Override
    public void initData() {
        departId = getIntent().getStringExtra("departId");
        companyId = getIntent().getStringExtra("companyId");
        datas = getIntent().getParcelableArrayListExtra("list");
        if (datas == null) {
            datas = new ArrayList<>();
        }
        SwipeUtil.openSwipe(this, dataBinding.recyclerView, (menuBridge, pos) -> {
            menuBridge.closeMenu();
            slimAdapterEx.getData().remove(pos);
            slimAdapterEx.notifyItemRemoved(pos);
        });
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapterEx = SlimAdapterEx.create()
                .register(R.layout.list_item_10, new SlimInjector<EmploymentListResultBean>() {
                    @Override
                    public void onInject(EmploymentListResultBean data, IViewInjector injector) {
                        injector.text(R.id.tv_upper_left_content, data.getStaffName())
                                .text(R.id.tv_upper_right_content, data.getDepartName())
                                .text(R.id.tv_down_right_content, data.getPostName())
                                .with(R.id.iv_head, v -> {
                                    ImageUtil.setCircleImage(data.getIphoto(), (ImageView) v);
                                })
                                .invisible(R.id.iv_enter);
                    }
                })
                .attachTo(dataBinding.recyclerView);
        ItemClickSupport.addTo(dataBinding.recyclerView)
                .setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
                    @Override
                    public void onItemClicked(RecyclerView recyclerView, int position, View v) {

//                        Intent intent = new Intent();
//                        intent.putExtra("content",slimAdapterEx.getDataItem(position).getStaffName());
//                        intent.putExtra("guid",slimAdapterEx.getDataItem(position).getGuid());
//                        setResult(Config.INTENT_CONTENT,intent);
//                        finish();
                    }
                });
        RxView.clicks(dataBinding.rlAdd)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribe(v -> {
                    Bundle bundle = new Bundle();
                    bundle.putString("departId", departId);
                    bundle.putString("companyId", companyId);
                    bundle.putParcelableArrayList("list", (ArrayList<EmploymentListResultBean>) slimAdapterEx.getData());
                    startActivityForResult(SelectSingleStaffActivity.class, bundle, Config.SELECT_SINGLE_STAFF);
                });
        slimAdapterEx.updateData(datas);
    }

    @Override
    public void setView() {
//
//        OkGo.<HttpResult<List<EmploymentListResultBean>>>post(HttpConfig.BASE_URL + HttpConfig.SELECT_EMPLOYMENT)
//                .params("departId", departId)
//                .params("companyId", companyId)
//                .execute(new DialogCallBack<HttpResult<List<EmploymentListResultBean>>>(this) {
//                    @Override
//                    public void onSuccess(Response<HttpResult<List<EmploymentListResultBean>>> response) {
//                        HttpResult<List<EmploymentListResultBean>> httpResult = response.body();
//                        ToastUtil.showShort(httpResult.getMsg());
//                        if (httpResult.getStatus() == 200) {
//                            slimAdapterEx.updateData(httpResult.getItems());
//                        }
//                    }
//                });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Config.INTENT_CONTENT) {
            slimAdapterEx.getData().add((EmploymentListResultBean) data.getSerializableExtra("bean"));
            slimAdapterEx.notifyItemInserted(datas.size() - 1);
        }
    }
}
